home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Demos / AirHockey / d3dFrame.cls < prev    next >
Encoding:
Visual Basic class definition  |  2001-10-08  |  32.9 KB  |  1,016 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "CD3DFrame"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14.  
  15. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  16. '
  17. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  18. '
  19. '  File:       D3DFrame.cls
  20. '  Content:    D3D Visual Basic Framework Frame object
  21. '
  22. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  23.  
  24.  
  25. Option Explicit
  26.  
  27. Public ObjectName As String             'Name of object
  28. Public Enabled As Boolean               'True to render
  29.  
  30. Dim m_Matrix As D3DMATRIX               'local coordinate system matrix
  31. Dim m_MatConcat As D3DMATRIX            'world matrix coordinate system
  32. Dim m_MatConcatInv As D3DMATRIX         'inv world matrix
  33.  
  34. Dim m_NumFrames As Long                 'Number of child frame
  35. Dim m_NumMesh As Long                   'Number of child mesh
  36. Dim m_MaxFrames As Long                 'Max number of frames before reallocation
  37. Dim m_MaxMesh As Long                   'Max number of meshes befor reallocation
  38. Dim m_DeletedFrames As Long             'Number of frame deleted
  39. Dim m_DeletedMesh As Long               'Number of mesh deleted
  40.  
  41. Dim m_D3DMesh() As CD3DMesh             'List of child Meshes
  42. Dim m_D3DFrame() As CD3DFrame           'List of Child Frame
  43.  
  44. Dim m_PreRenderEvent As DirectXEvent8   'Callback for prerender event
  45. Dim m_PreRenderId As Long               'Handle to send to prerender event
  46. Dim m_PostRenderEvent As DirectXEvent8  'Callback for postrender event
  47. Dim m_PostRenderId As Long              'Handle to send to postrender event
  48.  
  49. Dim m_SphereRadius As Single            'Bounding radius
  50. Dim m_SphereWorldPos As D3DVECTOR       'Bounding sphere center in world coord
  51. Dim m_SphereModelPos As D3DVECTOR       'Bounding sphere center in local coord
  52.  
  53. Dim m_pos As D3DVECTOR                  'Position as set by SetPosition
  54. Dim m_quat As D3DQUATERNION             'Orientations as set by SetOrientation
  55. Dim m_rotcenter As D3DVECTOR            '
  56. Dim m_scale As Single                   'Uniform scale as set by SetScale
  57.  
  58. Const kArrayStep = 5
  59.  
  60. Public Enum CombineTypeEnum
  61.     COMBINE_BEFORE = 0
  62.     COMBINE_AFTER = 1
  63.     COMBINE_replace = 2
  64. End Enum
  65.  
  66. Public Enum TransformOp
  67.     TRANSFORM_COMPUTE = 0
  68.     TRANSFORM_FROMUPDATEFRAME = 1
  69.     TRANSFORM_DISABLE = 2
  70. End Enum
  71.  
  72.  
  73. '-----------------------------------------------------------------------------
  74. ' Name: SetPreRenderCallback
  75. ' Desc: callback to be called before Frame renders children
  76. '-----------------------------------------------------------------------------
  77. Public Function SetPreRenderCallback(cb As DirectXEvent8, id As Long)
  78.     Set m_PreRenderEvent = cb
  79.     m_PreRenderId = id
  80. End Function
  81.  
  82. '-----------------------------------------------------------------------------
  83. ' Name: SetPostRenderCallback
  84. ' Desc: callback to be called after Frame renders children
  85. '-----------------------------------------------------------------------------
  86. Public Function SetPostRenderCallback(cb As DirectXEvent8, id As Long)
  87.     Set m_PostRenderEvent = cb
  88.     m_PostRenderId = id
  89. End Function
  90.  
  91.  
  92.  
  93. '-----------------------------------------------------------------------------
  94. ' Name: AddChild
  95. ' Desc: adds another frame as a child
  96. '-----------------------------------------------------------------------------
  97. Public Sub AddChild(child As CD3DFrame)
  98.     If m_MaxFrames = 0 Then
  99.         m_MaxFrames = kArrayStep
  100.         ReDim m_D3DFrame(m_MaxFrames)
  101.     ElseIf m_MaxFrames <= m_NumFrames Then
  102.         m_MaxFrames = m_MaxFrames + kArrayStep
  103.         ReDim Preserve m_D3DFrame(m_MaxFrames)
  104.     End If
  105.     Set m_D3DFrame(m_NumFrames) = child
  106.     m_NumFrames = m_NumFrames + 1
  107. End Sub
  108.  
  109. '-----------------------------------------------------------------------------
  110. ' Name: SetPosition
  111. ' Desc: sets position of the object in the parents coordinate system
  112. ' Note: will replace whatever matrix is set
  113. '-----------------------------------------------------------------------------
  114. Sub SetPosition(vec As D3DVECTOR)
  115.     m_pos = vec
  116.     UpdateMatrix
  117. End Sub
  118.  
  119. '-----------------------------------------------------------------------------
  120. ' Name: GetPosition
  121. ' Desc: returns the position of the object in the parents coordinate system
  122. '-----------------------------------------------------------------------------
  123. Function GetPosition() As D3DVECTOR
  124.     GetPosition.X = m_Matrix.m41
  125.     GetPosition.Y = m_Matrix.m42
  126.     GetPosition.z = m_Matrix.m43
  127.  
  128. End Function
  129.  
  130. '-----------------------------------------------------------------------------
  131. ' Name: SetOrientation
  132. ' Desc: sets the orientation of the object
  133. ' Note: use D3DXQuaternionRotationAxis to create the quaternion
  134. ' Note: will replace whatever matrix is set
  135. '-----------------------------------------------------------------------------
  136. Sub SetOrientation(quat As D3DQUATERNION)
  137.     m_quat = quat
  138.     UpdateMatrix
  139. End Sub
  140.  
  141. '-----------------------------------------------------------------------------
  142. ' Name: GetOrientation
  143. ' Desc: returns what ever was passed to SetOrientation
  144. ' Note: does not inspect the matrix to obtain orientation
  145. '-----------------------------------------------------------------------------
  146. Function GetOrientation() As D3DQUATERNION
  147.     GetOrientation = m_quat
  148. End Function
  149.  
  150.  
  151. '-----------------------------------------------------------------------------
  152. ' Name: SetScale
  153. ' Desc: Sets the uniform scale of all children
  154. ' Note: will replace whatever matrix is set
  155. '-----------------------------------------------------------------------------
  156. Sub SetScale(val As Single)
  157.     m_scale = val
  158.     UpdateMatrix
  159. End Sub
  160.  
  161. '-----------------------------------------------------------------------------
  162. ' Name: GetScale
  163. ' Desc: returns what ever was passed to SetScale
  164. ' Note: does not inspect the matrix to obtain scale
  165. '-----------------------------------------------------------------------------
  166. Function GetScale() As Single
  167.     GetScale = m_scale
  168. End Function
  169.  
  170.  
  171.  
  172. '-----------------------------------------------------------------------------
  173. ' Name: SetOrientationCenter
  174. ' Desc: Sets the pivot point for the Frame
  175. ' Note: will replace whatever matrix is set
  176. '-----------------------------------------------------------------------------
  177. Sub SetOrientationCenter(vec As D3DVECTOR)
  178.     m_rotcenter = vec
  179.     UpdateMatrix
  180. End Sub
  181.  
  182. '-----------------------------------------------------------------------------
  183. ' Name: GetOrientationCenter
  184. ' Desc: returns whatever pivot point was set
  185. ' Note: does not inspect the matrix to obtain pivot
  186. '-----------------------------------------------------------------------------
  187. Function GetOrientationCenter() As D3DVECTOR
  188.     GetOrientationCenter = m_rotcenter
  189. End Function
  190.  
  191.  
  192. '-----------------------------------------------------------------------------
  193. ' Name: AddMesh
  194. ' Desc: Adds a Child Mesh object to the frame
  195. '-----------------------------------------------------------------------------
  196. Public Sub AddMesh(childMesh As CD3DMesh)
  197.     If m_MaxMesh = 0 Then
  198.         m_MaxMesh = kArrayStep
  199.         ReDim m_D3DMesh(m_MaxMesh)
  200.     ElseIf m_MaxMesh <= m_NumMesh Then
  201.         m_MaxMesh = m_MaxMesh + kArrayStep
  202.         ReDim Preserve m_D3DMesh(m_MaxMesh)
  203.     End If
  204.     Set m_D3DMesh(m_NumMesh) = childMesh
  205.     m_NumMesh = m_NumMesh + 1
  206.     
  207.     
  208. End Sub
  209.  
  210.  
  211.  
  212. '-----------------------------------------------------------------------------
  213. ' Name: AddD3DXMesh
  214. ' Desc: Adds a D3DX mesh object to the frame
  215. ' Note: The d3dxmesh object is first encapuslated in a CD3DMesh object
  216. '-----------------------------------------------------------------------------
  217. Public Function AddD3DXMesh(mesh As D3DXMesh) As CD3DMesh
  218.     
  219.     Dim childMesh As CD3DMesh
  220.     Set childMesh = New CD3DMesh
  221.     childMesh.InitFromD3DXMesh mesh
  222.     
  223.     If m_MaxMesh = 0 Then
  224.         m_MaxMesh = kArrayStep
  225.         ReDim m_D3DMesh(m_MaxMesh)
  226.     ElseIf m_MaxMesh <= m_NumMesh Then
  227.         m_MaxMesh = m_MaxMesh + kArrayStep
  228.         ReDim Preserve m_D3DMesh(m_MaxMesh)
  229.     End If
  230.     Set m_D3DMesh(m_NumMesh) = childMesh
  231.     m_NumMesh = m_NumMesh + 1
  232.     
  233.     Set AddD3DXMesh = childMesh
  234. End Function
  235.  
  236. '-----------------------------------------------------------------------------
  237. ' Name: GetMatrix
  238. ' Desc: Returns the matrix for the local coordinate system
  239. '-----------------------------------------------------------------------------
  240. Public Function GetMatrix() As D3DMATRIX
  241.     GetMatrix = m_Matrix
  242. End Function
  243.  
  244. '-----------------------------------------------------------------------------
  245. ' Name: SetMatrix
  246. ' Desc: Sets the matrix for the local coordinate system
  247. ' Note: This overrides any previous calls to functions such as
  248. '       SetPosition, SetScale, SetOrientation
  249. '-----------------------------------------------------------------------------
  250. Public Function SetMatrix(m As D3DMATRIX)
  251.     m_Matrix = m
  252. End Function
  253.  
  254. '-----------------------------------------------------------------------------
  255. ' Name: GetChildMeshCount
  256. ' Desc: returns the number of child meshes
  257. '-----------------------------------------------------------------------------
  258. Public Function GetChildMeshCount()
  259.     GetChildMeshCount = m_NumMesh
  260. End Function
  261.  
  262.  
  263. '-----------------------------------------------------------------------------
  264. ' Name: GetChildMesh
  265. ' Desc: returns a given child mesh
  266. '-----------------------------------------------------------------------------
  267. Public Function GetChildMesh(i As Long) As CD3DMesh
  268.     Set GetChildMesh = m_D3DMesh(i)
  269. End Function
  270.  
  271. '-----------------------------------------------------------------------------
  272. ' Name: GetChildFrame
  273. ' Desc: returns a given child frame
  274. '-----------------------------------------------------------------------------
  275. Public Function GetChildFrame(i As Long) As CD3DFrame
  276.     Set GetChildFrame = m_D3DFrame(i)
  277. End Function
  278.  
  279.  
  280. '-----------------------------------------------------------------------------
  281. ' Name: GetChildFrameCount
  282. ' Desc: returns number of Child Frames
  283. '-----------------------------------------------------------------------------
  284. Public Function GetChildFrameCount()
  285.     GetChildFrameCount = m_NumFrames
  286. End Function
  287.  
  288.  
  289.  
  290. '-----------------------------------------------------------------------------
  291. ' Name: FindChildObject
  292. ' Desc: Given an object name (flags are ignored) return the first child with
  293. '       a matching name. use the ClassName property to see if its a mesh
  294. '       or a frame object
  295. '-----------------------------------------------------------------------------
  296.  
  297. Public Function FindChildObject(Name As String, flags As Long) As Object
  298.     Dim i As Long
  299.     Dim aMesh As CD3DMesh
  300.     Dim aFrame As CD3DFrame
  301.     
  302.     'check self
  303.     If ObjectName = Name Then
  304.         Set FindChildObject = Me
  305.         Exit Function
  306.     End If
  307.     
  308.     'check child mesh
  309.     For i = 0 To m_NumMesh - 1
  310.         Set aMesh = m_D3DMesh(i)
  311.         If Not aMesh Is Nothing Then
  312.             If aMesh.ObjectName = Name Then
  313.                 Set FindChildObject = aMesh
  314.                 Exit Function
  315.             End If
  316.         End If
  317.     Next
  318.     
  319.     Dim o As Object
  320.     'check child frames
  321.     For i = 0 To m_NumFrames - 1
  322.         Set aFrame = m_D3DFrame(i)
  323.         If Not aFrame Is Nothing Then
  324.             Set o = aFrame.FindChildObject(Name, flags)
  325.             If Not o Is Nothing Then
  326.                 Set FindChildObject = o
  327.                 Exit Function
  328.             End If
  329.         End If
  330.     Next
  331.     Set FindChildObject = Nothing
  332. End Function
  333.  
  334. '-----------------------------------------------------------------------------
  335. ' Name: FindChildParent
  336. ' Desc: given an object name find its parent frame
  337. '-----------------------------------------------------------------------------
  338.  
  339. Public Function FindChildParent(Name As String, flags As Long) As CD3DFrame
  340.     Dim i As Long
  341.     Dim aMesh As CD3DMesh
  342.     Dim aFrame As CD3DFrame
  343.     
  344.     'check child mesh
  345.     For i = 0 To m_NumMesh - 1
  346.         Set aMesh = m_D3DMesh(i)
  347.         If Not aMesh Is Nothing Then
  348.             If aMesh.ObjectName = Name Then
  349.                 Set FindChildParent = Me
  350.                 Exit Function
  351.             End If
  352.         End If
  353.     Next
  354.     
  355.     Dim o As Object
  356.     'check child frames
  357.     For i = 0 To m_NumFrames - 1
  358.         Set aFrame = m_D3DFrame(i)
  359.         If Not aFrame Is Nothing Then
  360.             If aFrame.ObjectName = Name Then
  361.                 FindChildParent = Me
  362.             End If
  363.             
  364.             Set o = aFrame.FindChildParent(Name, flags)
  365.             If Not o Is Nothing Then
  366.                 Set FindChildParent = o
  367.                 Exit Function
  368.             End If
  369.         End If
  370.     Next
  371.     
  372.     Set FindChildParent = Nothing
  373.     
  374. End Function
  375.  
  376.  
  377. '-----------------------------------------------------------------------------
  378. ' Name: Destroy
  379. ' Desc: Release all references
  380. '-----------------------------------------------------------------------------
  381. Public Sub Destroy()
  382.     ReDim m_D3DMesh(0)
  383.     ReDim m_D3DFrame(0)
  384.     m_NumFrames = 0
  385.     m_NumMesh = 0
  386. End Sub
  387.  
  388.  
  389.  
  390. '-----------------------------------------------------------------------------
  391. ' Name: InitFromXOF
  392. ' Desc: called from InitFromFile
  393. '-----------------------------------------------------------------------------
  394. Friend Sub InitFromXOF(dev As Direct3DDevice8, FileData As DirectXFileData, parent As CD3DFrame)
  395.     
  396.     Dim ChildData As DirectXFileData
  397.     Dim ChildObj As DirectXFileObject
  398.     Dim TypeGuid As String
  399.     Dim newmesh As CD3DMesh
  400.     Dim newFrame As CD3DFrame
  401.  
  402.     If Not parent Is Nothing Then parent.AddChild Me
  403.     
  404.     ObjectName = FileData.GetName()
  405.     
  406.     Do
  407.         'list of object - should return nothing at end of list
  408.         Set ChildData = FileData.GetNextObject()
  409.         If ChildData Is Nothing Then Exit Do
  410.     
  411.         Select Case ChildData.GetType()
  412.                
  413.             Case "TID_D3DRMFrame"
  414.                 Set newFrame = New CD3DFrame
  415.                 newFrame.InitFromXOF dev, ChildData, Me
  416.                 Set newFrame = Nothing
  417.   
  418.             Case "TID_D3DRMMesh"
  419.                 Set newmesh = New CD3DMesh
  420.                 newmesh.InitFromXOF dev, ChildData
  421.                 AddMesh newmesh
  422.                 Set newmesh = Nothing
  423.   
  424.             Case "TID_D3DRMFrameTransformMatrix"
  425.                 ChildData.GetDataFromOffset "", 0, Len(m_Matrix), m_Matrix
  426.             
  427.         End Select
  428.             
  429.     Loop
  430.     
  431. End Sub
  432.  
  433.  
  434. '-----------------------------------------------------------------------------
  435. ' Name: InitFromFile
  436. ' Desc: Called from D3DUtil_LoadFromFile
  437. '-----------------------------------------------------------------------------
  438. Public Function InitFromFile(dev As Direct3DDevice8, strFile As String, parent As CD3DFrame, animParent As CD3DAnimation) As Boolean
  439.  
  440.  
  441.     Dim xfile As DirectXFile
  442.     Dim enumX As DirectXFileEnum
  443.     Dim ChildData As DirectXFileData
  444.     Dim rootid As Long
  445.     Dim childid As Long
  446.     Dim strPath As String
  447.     Dim newmesh As CD3DMesh
  448.     Dim newFrame As CD3DFrame
  449.     
  450.     On Local Error GoTo errOut
  451.                 
  452.     
  453.     'create the X file object
  454.     Set xfile = g_dx.DirectXFileCreate()
  455.     
  456.     'make sure it understand MeshBuilders etc
  457.     xfile.RegisterDefaultTemplates
  458.     
  459.     On Local Error Resume Next
  460.     
  461.     'Open the file - will propegate file not found if fails
  462.     Set enumX = xfile.CreateEnumObject(strFile)
  463.     If Err.Number <> 0 Then
  464.     
  465.         On Local Error GoTo errOut
  466.         strPath = g_mediaPath + strFile
  467.         Set enumX = xfile.CreateEnumObject(strPath)
  468.     
  469.     End If
  470.     
  471.             
  472.     'Inspect each top level object in the file
  473.     Do
  474.         Set ChildData = enumX.GetNextDataObject()
  475.         If ChildData Is Nothing Then Exit Do
  476.                 
  477.         If ChildData.GetType = "TID_D3DRMFrame" Then
  478.             Set newFrame = New CD3DFrame
  479.             newFrame.InitFromXOF dev, ChildData, Me
  480.             Set newFrame = Nothing
  481.             
  482.         ElseIf ChildData.GetType = "TID_D3DRMMesh" Then
  483.             Set newmesh = New CD3DMesh
  484.             newmesh.InitFromXOF dev, ChildData
  485.             AddMesh newmesh
  486.             Set newmesh = Nothing
  487.             
  488.         ElseIf ChildData.GetType = "TID_D3DRMAnimationSet" Then
  489.             'TODO Parse Animation Set Data
  490.             If Not animParent Is Nothing Then
  491.                 animParent.ParseAnimSet ChildData, Me
  492.             End If
  493.         ElseIf ChildData.GetType = "TID_D3DRMMaterialSet" Then
  494.             'TODO Parse TopLevel Material Data
  495.             
  496.         End If
  497.     Loop
  498.  
  499.     If Not parent Is Nothing Then
  500.         parent.AddChild Me
  501.     End If
  502.                     
  503.     InitFromFile = True
  504.     Exit Function
  505.     
  506. errOut:
  507.     InitFromFile = False
  508.     
  509. End Function
  510.  
  511.  
  512.  
  513.  
  514.  
  515. '-----------------------------------------------------------------------------
  516. ' Name: SetFVF
  517. ' Desc: Changes the Flexible Vertex Format of all child objects
  518. '-----------------------------------------------------------------------------
  519. Public Sub SetFVF(dev As Direct3DDevice8, fvf As Long)
  520.     Dim aMesh As CD3DMesh
  521.     Dim aFrame As CD3DFrame
  522.     Dim i As Long
  523.     
  524.     For i = 0 To m_NumMesh - 1
  525.         Set aMesh = m_D3DMesh(i)
  526.         If Not aMesh Is Nothing Then
  527.             aMesh.SetFVF g_dev, fvf
  528.         End If
  529.     Next
  530.     
  531.     For i = 0 To m_NumFrames - 1
  532.         Set aFrame = m_D3DFrame(i)
  533.         If Not aFrame Is Nothing Then
  534.             aFrame.SetFVF g_dev, fvf
  535.         End If
  536.     Next
  537.  
  538. End Sub
  539.  
  540.  
  541. '-----------------------------------------------------------------------------
  542. ' Name: FlipNormals
  543. ' Desc: Flips the normals on all child objects (provided they are of D3DFVF_VERTEX)
  544. '-----------------------------------------------------------------------------
  545. Public Sub FlipNormals()
  546.     Dim aMesh As CD3DMesh
  547.     Dim aFrame As CD3DFrame
  548.     Dim i As Long
  549.     
  550.     For i = 0 To m_NumMesh - 1
  551.         Set aMesh = m_D3DMesh(i)
  552.         If Not aMesh Is Nothing Then
  553.             aMesh.FlipNormals
  554.         End If
  555.     Next
  556.     
  557.     For i = 0 To m_NumFrames - 1
  558.         Set aFrame = m_D3DFrame(i)
  559.         If Not aFrame Is Nothing Then
  560.             aFrame.FlipNormals
  561.         End If
  562.     Next
  563.  
  564. End Sub
  565.  
  566.  
  567. '-----------------------------------------------------------------------------
  568. ' Name: ComputeNormals
  569. ' Desc: Computes normals on all child objects (provided they have a normal set set)
  570. '-----------------------------------------------------------------------------
  571. Public Sub ComputeNormals()
  572.     Dim aMesh As CD3DMesh
  573.     Dim aFrame As CD3DFrame
  574.     Dim i As Long
  575.     
  576.     For i = 0 To m_NumMesh - 1
  577.         Set aMesh = m_D3DMesh(i)
  578.         If Not aMesh Is Nothing Then
  579.             aMesh.ComputeNormals
  580.         End If
  581.     Next
  582.     
  583.     For i = 0 To m_NumFrames - 1
  584.         Set aFrame = m_D3DFrame(i)
  585.         If Not aFrame Is Nothing Then
  586.             aFrame.ComputeNormals
  587.         End If
  588.     Next
  589.  
  590. End Sub
  591.  
  592.  
  593. '-----------------------------------------------------------------------------
  594. ' Name: Optimize
  595. ' Desc:
  596. '-----------------------------------------------------------------------------
  597. Public Sub Optimize()
  598.     Dim aMesh As CD3DMesh
  599.     Dim aFrame As CD3DFrame
  600.     Dim i As Long
  601.     
  602.     For i = 0 To m_NumMesh - 1
  603.         Set aMesh = m_D3DMesh(i)
  604.         If Not aMesh Is Nothing Then
  605.             aMesh.Optimize
  606.         End If
  607.     Next
  608.     
  609.     For i = 0 To m_NumFrames - 1
  610.         Set aFrame = m_D3DFrame(i)
  611.         If Not aFrame Is Nothing Then
  612.             aFrame.Optimize
  613.         End If
  614.     Next
  615.  
  616. End Sub
  617.  
  618.  
  619.  
  620. '-----------------------------------------------------------------------------
  621. ' Name: ComputeBoundingVolumes
  622. ' Desc: ComputesBoundingVolumes for all child objects
  623. '-----------------------------------------------------------------------------
  624. Public Sub ComputeBoundingVolumes()
  625.         
  626.     Dim aMesh As CD3DMesh
  627.     Dim aFrame As CD3DFrame
  628.     Dim i As Long
  629.     
  630.     For i = 0 To m_NumMesh - 1
  631.         Set aMesh = m_D3DMesh(i)
  632.         If Not aMesh Is Nothing Then
  633.             aMesh.ComputeBoundingVolumes
  634.         End If
  635.     Next
  636.     
  637.     For i = 0 To m_NumFrames - 1
  638.         Set aFrame = m_D3DFrame(i)
  639.         If Not aFrame Is Nothing Then
  640.             aFrame.ComputeBoundingVolumes
  641.         End If
  642.     Next
  643.  
  644. End Sub
  645.  
  646.  
  647. '-----------------------------------------------------------------------------
  648. ' Name: UpdateFrames
  649. ' Desc: Called to precompute the WorldMatrices for each child object
  650. ' Note: Must be used prior to calling Mesh.RenderSkin
  651. '-----------------------------------------------------------------------------
  652. Public Sub UpdateFrames()
  653.     ComputeChildMatricesEx g_identityMatrix
  654. End Sub
  655.  
  656. '-----------------------------------------------------------------------------
  657. ' Name: GetUpdatedMatrix
  658. ' Desc: Returns the precomputed matrix
  659. '-----------------------------------------------------------------------------
  660. Public Function GetUpdatedMatrix() As D3DMATRIX
  661.     GetUpdatedMatrix = m_MatConcat
  662. End Function
  663.  
  664.  
  665. '-----------------------------------------------------------------------------
  666. ' Name: ComputeChildMatricesEx
  667. ' Desc: Aux function for UpdateFrames
  668. '-----------------------------------------------------------------------------
  669. Friend Sub ComputeChildMatricesEx(matSavedWorld As D3DMATRIX)
  670.     
  671.     
  672.     Dim aFrame As CD3DFrame
  673.     Dim i As Long
  674.     Dim det As Single
  675.     
  676.     Call D3DXMatrixMultiply(m_MatConcat, m_Matrix, matSavedWorld)
  677.     Call D3DXMatrixInverse(m_MatConcatInv, det, m_MatConcat)
  678.            
  679.     For i = 0 To m_NumFrames - 1
  680.         Set aFrame = m_D3DFrame(i)
  681.         If Not aFrame Is Nothing Then
  682.             aFrame.ComputeChildMatricesEx m_MatConcat
  683.         End If
  684.     Next
  685.  
  686. End Sub
  687.  
  688.  
  689.  
  690.  
  691.  
  692. '-----------------------------------------------------------------------------
  693. ' Name: RenderEx
  694. ' Desc: Render Child Objects
  695. ' Params:
  696. '       dev                     Device to render to
  697. '       bDrawOpaqueSubsets      Render all objects with an material alpha of 1
  698. '       bDrawAlphaSubsets       Render all objects with transparent alpha
  699. '       transform
  700. '                               TRANSFORM_COMPUT indicates to calculate world matrix
  701. '                               TRANSFORM_FROMUPDATEFRAME inidcates to use precomputed matrix
  702. '                               TRANSFORM_DISABLE does not set any world matrix
  703. '                               and uses what ever has been currently set
  704. '-----------------------------------------------------------------------------
  705. Friend Sub RenderEx(dev As Direct3DDevice8, bDrawOpaqueSubsets As Boolean, bDrawAlphaSubsets As Boolean, transform As TransformOp)
  706.     
  707.     Dim matSavedWorld As D3DMATRIX
  708.     Dim matWorld As D3DMATRIX
  709.     Dim aFrame As CD3DFrame
  710.     Dim aMesh As CD3DMesh
  711.     Dim vCenter As D3DVECTOR
  712.     
  713.     If Not Enabled Then Exit Sub
  714.     
  715.     Dim i As Long
  716.     
  717.     
  718.     If transform = TRANSFORM_COMPUTE Then
  719.         Call g_dev.GetTransform(D3DTS_WORLD, matSavedWorld)
  720.         Call D3DXMatrixMultiply(matWorld, m_Matrix, matSavedWorld)
  721.         Call g_dev.SetTransform(D3DTS_WORLD, matWorld)
  722.     ElseIf transform = TRANSFORM_FROMUPDATEFRAME Then
  723.         Call g_dev.SetTransform(D3DTS_WORLD, m_MatConcat)
  724.     End If
  725.     
  726.     
  727.     If Not m_PreRenderEvent Is Nothing Then
  728.         m_PreRenderEvent.DXCallback m_PreRenderId
  729.     End If
  730.     
  731.     For i = 0 To m_NumMesh - 1
  732.         Set aMesh = m_D3DMesh(i)
  733.         If Not aMesh Is Nothing Then
  734.             
  735.             If g_bClipMesh And aMesh.bHasSphere Then
  736.                 D3DXVec3TransformCoord vCenter, aMesh.SphereCenter, matWorld
  737.                 If D3DUtil_IsSphereVisible(vCenter, aMesh.SphereRadius) <> 0 Then
  738.                     aMesh.RenderEx g_dev, bDrawOpaqueSubsets, bDrawAlphaSubsets
  739.                 Else
  740.                     'Debug.Print "cull " + aMesh.ObjectName
  741.                 End If
  742.             Else
  743.                 aMesh.RenderEx g_dev, bDrawOpaqueSubsets, bDrawAlphaSubsets
  744.             End If
  745.         End If
  746.     Next
  747.     
  748.     For i = 0 To m_NumFrames - 1
  749.         Set aFrame = m_D3DFrame(i)
  750.         If Not aFrame Is Nothing Then
  751.             aFrame.RenderEx g_dev, bDrawOpaqueSubsets, bDrawAlphaSubsets, transform
  752.         End If
  753.     Next
  754.         
  755.     If transform = TRANSFORM_COMPUTE Then
  756.         Call g_dev.SetTransform(D3DTS_WORLD, matSavedWorld)
  757.     End If
  758.     
  759.     If Not m_PostRenderEvent Is Nothing Then
  760.         m_PostRenderEvent.DXCallback m_PostRenderId
  761.     End If
  762.  
  763.  
  764. End Sub
  765.  
  766.  
  767.  
  768.  
  769. '-----------------------------------------------------------------------------
  770. ' Name: RenderSkins
  771. ' Desc: Searches the children for any mesh with skinned objects
  772. '       and renders them
  773. ' Note: BoneMatrice must be precalulated before rendering
  774. '       and bones must also be attached to appropriate mesh
  775. '-----------------------------------------------------------------------------
  776. Friend Sub RenderSkins()
  777.     Dim i As Long
  778.     Dim aMesh As CD3DMesh
  779.     Dim aFrame As CD3DFrame
  780.     
  781.     If Not m_PreRenderEvent Is Nothing Then
  782.         m_PreRenderEvent.DXCallback m_PreRenderId
  783.     End If
  784.         
  785.     For i = 0 To m_NumMesh - 1
  786.         Set aMesh = m_D3DMesh(i)
  787.         If Not aMesh Is Nothing Then
  788.             aMesh.RenderSkin
  789.         End If
  790.     Next
  791.     
  792.     For i = 0 To m_NumFrames - 1
  793.         Set aFrame = m_D3DFrame(i)
  794.         If Not aFrame Is Nothing Then
  795.             aFrame.RenderSkins
  796.         End If
  797.     Next
  798.     
  799.     If Not m_PostRenderEvent Is Nothing Then
  800.         m_PostRenderEvent.DXCallback m_PostRenderId
  801.     End If
  802.  
  803. End Sub
  804.  
  805. '-----------------------------------------------------------------------------
  806. ' Name: AttatchBonesToMesh
  807. ' Desc:
  808. '-----------------------------------------------------------------------------
  809. Friend Sub AttatchBonesToMesh(parent As CD3DFrame)
  810.     
  811.     Dim i As Long
  812.     Dim aMesh As CD3DMesh
  813.     Dim aFrame As CD3DFrame
  814.     
  815.         
  816.     For i = 0 To m_NumMesh - 1
  817.         Set aMesh = m_D3DMesh(i)
  818.         If Not aMesh Is Nothing Then
  819.             aMesh.AttatchBonesToMesh parent
  820.         End If
  821.     Next
  822.     
  823.     For i = 0 To m_NumFrames - 1
  824.         Set aFrame = m_D3DFrame(i)
  825.         If Not aFrame Is Nothing Then
  826.             aFrame.AttatchBonesToMesh parent
  827.         End If
  828.     Next
  829.     
  830. End Sub
  831.  
  832. '-----------------------------------------------------------------------------
  833. ' Name: Render
  834. ' Desc: render all child objects
  835. '-----------------------------------------------------------------------------
  836. Public Sub Render(dev As Direct3DDevice8)
  837.         
  838.     dev.SetTransform D3DTS_WORLD, g_identityMatrix
  839.         
  840.     'Render opaque subsets in  the meshes
  841.     RenderEx g_dev, True, False, TRANSFORM_COMPUTE
  842.         
  843.     
  844.     ' Enable alpha blending
  845.     Call dev.SetRenderState(D3DRS_ALPHABLENDENABLE, 1)
  846.     Call dev.SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA)
  847.     Call dev.SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA)
  848.  
  849.     dev.SetTransform D3DTS_WORLD, g_identityMatrix
  850.     
  851.     RenderEx g_dev, False, True, TRANSFORM_COMPUTE
  852.         
  853.     Call dev.SetRenderState(D3DRS_ALPHABLENDENABLE, 0)
  854.     
  855. End Sub
  856.  
  857.  
  858. '-----------------------------------------------------------------------------
  859. ' Name: ClassName
  860. '-----------------------------------------------------------------------------
  861. Public Function ClassName() As String
  862.     ClassName = "CD3DFrame"
  863. End Function
  864.  
  865.  
  866. '-----------------------------------------------------------------------------
  867. ' Name: Class_Initialize()
  868. '-----------------------------------------------------------------------------
  869. Private Sub Class_Initialize()
  870.     D3DXMatrixIdentity m_Matrix
  871.     D3DXMatrixIdentity g_identityMatrix
  872.         
  873.     Call D3DXQuaternionIdentity(m_quat)
  874.     m_scale = 1
  875.     Enabled = True
  876.     
  877. End Sub
  878.  
  879. '-----------------------------------------------------------------------------
  880. ' Name: AddRotation
  881. ' Param:
  882. '       combinetype     PreMultiply/PostMultiply/or replace current matrix
  883. '       x,y,z           axis of rotation
  884. '       rot             radians to rotate
  885. '-----------------------------------------------------------------------------
  886. Public Function AddRotation(combinetype As CombineTypeEnum, X As Single, Y As Single, z As Single, rot As Single)
  887.     
  888.     Dim mat As D3DMATRIX
  889.     D3DXMatrixRotationAxis mat, vec3(X, Y, z), rot
  890.     
  891.     Select Case combinetype
  892.         Case COMBINE_BEFORE
  893.             D3DXMatrixMultiply m_Matrix, mat, m_Matrix
  894.         Case COMBINE_AFTER
  895.             D3DXMatrixMultiply m_Matrix, m_Matrix, mat
  896.         Case COMBINE_replace
  897.             m_Matrix = mat
  898.     End Select
  899.  
  900. End Function
  901.  
  902.  
  903. '-----------------------------------------------------------------------------
  904. ' Name: AddScale
  905. ' Param:
  906. '       combinetype     PreMultiply/PostMultiply/or replace current matrix
  907. '       x,y,z           scale
  908. '-----------------------------------------------------------------------------
  909. Public Function AddScale(combinetype As CombineTypeEnum, X As Single, Y As Single, z As Single)
  910.     
  911.     Dim mat As D3DMATRIX
  912.     D3DXMatrixScaling mat, X, Y, z
  913.     Select Case combinetype
  914.         Case COMBINE_BEFORE
  915.             D3DXMatrixMultiply m_Matrix, mat, m_Matrix
  916.         Case COMBINE_AFTER
  917.             D3DXMatrixMultiply m_Matrix, m_Matrix, mat
  918.         Case COMBINE_replace
  919.             m_Matrix = mat
  920.     End Select
  921.  
  922. End Function
  923.  
  924. '-----------------------------------------------------------------------------
  925. ' Name: AddTranslation
  926. ' Param:
  927. '       combinetype     PreMultiply/PostMultiply/or replace current matrix
  928. '       x,y,z           translation
  929. '-----------------------------------------------------------------------------
  930. Public Function AddTranslation(combinetype As CombineTypeEnum, X As Single, Y As Single, z As Single)
  931.     
  932.     Dim mat As D3DMATRIX
  933.     D3DXMatrixTranslation mat, X, Y, z
  934.     Select Case combinetype
  935.         Case COMBINE_BEFORE
  936.             D3DXMatrixMultiply m_Matrix, mat, m_Matrix
  937.         Case COMBINE_AFTER
  938.             D3DXMatrixMultiply m_Matrix, m_Matrix, mat
  939.         Case COMBINE_replace
  940.             m_Matrix = mat
  941.     End Select
  942.  
  943. End Function
  944.  
  945.  
  946. '-----------------------------------------------------------------------------
  947. ' Name: TransformCoord
  948. ' Desc: Transform a vector by the pre-computed matrix
  949. '-----------------------------------------------------------------------------
  950. Public Function TransformCoord(vIn As D3DVECTOR) As D3DVECTOR
  951.     Dim vOut As D3DVECTOR
  952.     D3DXVec3TransformCoord vOut, vIn, m_MatConcat
  953.     TransformCoord = vOut
  954. End Function
  955.  
  956. '-----------------------------------------------------------------------------
  957. ' Name: TransformNormal
  958. ' Desc: Transform a normal by the pre-computed matrix
  959. '-----------------------------------------------------------------------------
  960. Public Function TransformNormal(vIn As D3DVECTOR) As D3DVECTOR
  961.     Dim vOut As D3DVECTOR
  962.     D3DXVec3TransformNormal vOut, vIn, m_MatConcat
  963.     TransformNormal = vOut
  964. End Function
  965.  
  966.  
  967. '-----------------------------------------------------------------------------
  968. ' Name: InverseTransformCoord
  969. ' Desc: Transform a vector by the inverse of the pre-computed matrix
  970. '-----------------------------------------------------------------------------
  971. Public Function InverseTransformCoord(vIn As D3DVECTOR) As D3DVECTOR
  972.     Dim vOut As D3DVECTOR
  973.     D3DXVec3TransformCoord vOut, vIn, m_MatConcatInv
  974.     InverseTransformCoord = vOut
  975. End Function
  976.  
  977. '-----------------------------------------------------------------------------
  978. ' Name: InverseTransformNormal
  979. ' Desc: Transform a normal by the inverse of the pre-computed matrix
  980. '-----------------------------------------------------------------------------
  981. Public Function InverseTransformNormal(vIn As D3DVECTOR) As D3DVECTOR
  982.     Dim vOut As D3DVECTOR
  983.     D3DXVec3TransformNormal vOut, vIn, m_MatConcatInv
  984.     InverseTransformNormal = vOut
  985. End Function
  986.  
  987.  
  988. '-----------------------------------------------------------------------------
  989. ' Name: InvalidateDeviceObjects
  990. ' Desc:
  991. '-----------------------------------------------------------------------------
  992. Public Sub InvalidateDeviceObjects()
  993.     'all objects are managed so nothing to do
  994. End Sub
  995.  
  996.  
  997. '-----------------------------------------------------------------------------
  998. ' Name: RestoreDeviceObjects
  999. ' Desc:
  1000. '-----------------------------------------------------------------------------
  1001. Public Sub RestoreDeviceObjects(dev As Direct3DDevice8)
  1002.     'all objects are managed so nothing to do
  1003. End Sub
  1004.  
  1005.  
  1006. '-----------------------------------------------------------------------------
  1007. ' Name: UpdateMatrix
  1008. ' Desc: Aux function for SetPosition, SetOrientation, SetScale
  1009. '-----------------------------------------------------------------------------
  1010. Private Sub UpdateMatrix()
  1011.     On Local Error Resume Next
  1012.     D3DXMatrixAffineTransformation m_Matrix, m_scale, m_rotcenter, m_quat, m_pos
  1013. End Sub
  1014.  
  1015.  
  1016.